<?php
declare (strict_types = 1);

namespace app\appcenter\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class TaskHook extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('taskhook')
        ->addArgument('app', Argument::OPTIONAL, "your app")
        ->addArgument('method', Argument::OPTIONAL, "your method")
        ->setDescription('taskhook Command');
    }
    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        if(empty($input->getArgument('app'))){
            $output->writeln("Success");
            return ;
        }
        $app = trim($input->getArgument('app'));
        if($app=='auto'){
            $dir_path = base_path()."plugin";
            if(file_exists($dir_path))
            {   //查询应用目录内所有应用
                $file_list = array_diff(scandir($dir_path), array('.', '..'));
                //循环调用插件方法
                foreach($file_list as $v)
                {
                    $class = "app\\plugin\\".$v."\\\command\hook"::class;
                    //if(class_exists($class)){
                    if (method_exists($class,'hook_auto')) {
                        (new $class())->hook_auto();
                    }
    
                }
            }


        }
        if(empty($input->getArgument('method'))){
            $output->writeln("Success");
            return ;
        }
        $method = trim($input->getArgument('method'));
        if($app && $method){

            $class = "app\\plugin\\".$app."\\command\\hook".''::class;
            if (method_exists($class,$method)) {
                $class::$method();
            }
        }
        $output->writeln("Success");
    }
}
